/**
  * @file		crc.c
  * @author		Krubkib
  * @version	V1.0.0
  * @date		October 13, 2013
  * @brief		Collection of helper functions 
  */

/* Includes ------------------------------------------------------------------*/
#include "helper.h"
#include "crc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
crc
crcSlow(uint8_t const message[], int nBytes)
{
    crc remainder = INITIAL_REMAINDER;	
		int byte;
	  uint16_t bit;
    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (byte = 0; byte < nBytes; ++byte)
    {
        /*
         * Bring the next byte into the remainder.
         */
        remainder ^= (message[byte] << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    return (remainder ^ FINAL_XOR_VALUE);

}   /* crcSlow() */

crc  crcTable[256];

void crcInit(void)
{
    crc remainder = INITIAL_REMAINDER;
		int dividend;
		uint8_t bit ;
    /*
     * Compute the remainder of each possible dividend.
     */
    for (dividend = 0; dividend < 256; ++dividend)
    {
        /*
         * Start with the dividend followed by zeros.
         */
        remainder = dividend << (WIDTH - 8);

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */			
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }

        /*
         * Store the result into the table.
         */
        crcTable[dividend] = remainder;
    }

}   /* crcInit() */
crc crcFast(uint8_t const message[], int nBytes)
{
    uint8_t data;
    crc remainder = INITIAL_REMAINDER;
		int byte;

    /*
     * Divide the message by the polynomial, a byte at a time.
     */
    for (byte = 1; byte < nBytes; ++byte)
    {
        data = message[byte] ^ (remainder >> (WIDTH - 8));
        remainder = crcTable[data] ^ (remainder << 8);
    }

    /*
     * The final remainder is the CRC.
     */
    return (remainder ^ FINAL_XOR_VALUE);

}   /* crcFast() */
//  crc remainder = INITIAL_REMAINDER; 
//return (remainder ^ FINAL_XOR_VALUE);
